package alf.mapping.view.touch;

import alf.mapping.view.Env;
import alf.mapping.view.activity.ItemEditionActivity;
import alf.mapping.view.visuals.Sprite;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.SystemClock;
import android.view.MotionEvent;
import java.util.List;

public class ResizingTouchReactor extends AbstractTouchReactor {

	private Rect pos;
	private boolean zooming;

	private Rect ref;

	@Override
	protected boolean onTouch(MotionEvent event, Point punto, Rect puntogordo) {
		String msg = null;
		int index = event.getActionIndex();
		Rect selector = callback.getSelector();
		switch (event.getActionMasked()) {
			case MotionEvent.ACTION_DOWN:
				//      msg = "down";
				pos = new Rect(punto.x, punto.y, 0, 0);
				Sprite touched = spriteAtPos(puntogordo);
				if (touched == null) {
				//	editor.select(null);
					return true;// recibir mas
				} else {
					if (editor.getSelected() != null
						&& editor.getSelected().contains(touched)) {
						msg = "resizing selected items";
					} else {
						editor.select(touched);
						msg = "resizing " + touched.getItem().getName();
					}
				}
				break;
			case MotionEvent.ACTION_POINTER_DOWN:
				//	msg = "pointer-down";
				pos.right = (int) event.getX(index);
				pos.bottom = (int) event.getY(index);
				selector = editor.scr2model(pos);
				selector.sort();
				this.ref = new Rect(selector);
				msg = "resizing selected items";
				
				zooming = true;

				break;
			case MotionEvent.ACTION_MOVE:
				if (zooming) {
					if (selector != null) {
						if (event.getPointerCount() > 1) {
							pos.left = punto.x;
							pos.top = punto.y;
							pos.right = (int) event.getX(1);
							pos.bottom = (int) event.getY(1);
							selector = editor.scr2model(pos);
							selector.sort();
							transformSelected(this.ref,selector);
							this.ref = new Rect(selector);
						}
					}
				} else {
					if (editor.getSelected() != null) {
						pos.right = (punto.x);
						pos.bottom = punto.y;
						float distancia = distance(pos);
						if (distancia >= MOVING_STEP) {
							for (Sprite selSprite: editor.getSelected()) {
								selSprite.rect().offset((int)(zoomFactor() * pos.width()), (int)(zoomFactor() * pos.height()));
							}
							pos.left = pos.right;
							pos.top = pos.bottom;
						}
					}
				}
				break;
			case MotionEvent.ACTION_POINTER_UP:
				//msg = "pointer.up";
				if (zooming) {
					if (selector != null) {
						pos.left = (int) event.getX(0);
						pos.top = (int) event.getY(0);
						pos.right = (int) event.getX(1);
						pos.bottom = (int) event.getY(1);
						selector = editor.scr2model(pos);
						selector.sort();
						transformSelected(this.ref,selector);
						selector = null;
					} else {
						zooming = false;
					}
				}
				break;	
			case MotionEvent.ACTION_UP:
				if (zooming) {
					if (selector != null) {
						selector = null;
					}
					zooming = false;
				} else {
					pos.right = punto.x;
					pos.bottom = punto.y;
					if (editor.getSelected() == null) {
						oldSel = null;
						//      Env.toast(getContext(),selected.getItem().getName());
					} else {
						for (Sprite selSprite: editor.getSelected()) {
							selSprite.rect().offset((int)(zoomFactor() * pos.width()), (int)(zoomFactor() * pos.height()));
						}
					}
				}
				break;
		
		}
		if (msg != null) {      
			Env.setStatus(msg + (index > 0 ?"(" + index + ")": ""));
		}
		callback.setSelector(selector);
		return true;
	}

	private void transformSelected(Rect ref, Rect selector) {
		List<Sprite> selectedSprites = editor.getSelected();
		Rect rdif = new Rect(selector.bottom-ref.bottom,
							 selector.top-ref.top,
							 selector.right-ref.right,
							 selector.bottom-ref.bottom);
		Env.debug("resize rdif:"+rdif);
		if (selectedSprites!=null){
			for (Sprite sp : selectedSprites){
				Rect r = sp.rect();
				r.top += rdif.top;//*r.height()/selector.height();
				r.left += rdif.left;//*r.width()/selector.width();
				r.bottom += rdif.bottom;//*r.height()/selector.height();
				r.right += rdif.right;//*r.width()/selector.width();
				if (r.width()==0) {
					r.right++;
				}
				if (r.height()==0){
					r.bottom++;
				}
				r.sort();
				//selSprite.rect().offset((int)(zoomFactor() * pos.width()), (int)(zoomFactor() * pos.height()));
			}
		}
	}

	
}
